<template>
  <div class="box">
    <van-nav-bar title="客服" left-text="返回" left-arrow @click-left="onClickLeft" />

    <div class="content">
      <div class="ctBBox">
        <div class="item" v-for="item in robot" :key="item.index">
          <div class="robot" v-if="(item.type == 'robot')">
            <div class="pic">
              <var-avatar hoverable
                src='https://cnbj0.fds.api.xiaomi.com/b2c-kfs-media/png221114-b2761e59775e64e0f1fed476effc7daf.png' />
            </div>
            <div class="robotCtx">
              <div class="ct">
                {{ item.content }}
              </div>
              <div class="time">
                {{ item.time }}
              </div>
            </div>
          </div>
          <div class="self" v-if="(item.type == 'self')">
            <div class="pic">
              <var-avatar hoverable
                src='https://cnbj1.fds.api.xiaomi.com/user-avatar/a3a66fd4-2789-49b8-8108-4a79e24001d3.jpg' />
            </div>
            <div class="selfCtx">
              <div class="ct">
                {{ item.content }}
              </div>
              <div class="time">
                {{ item.time }}
              </div>
            </div>

          </div>
        </div>

      </div>
      <div class="history" ref="chatContainer">
        <div style="text-align: center;">客服</div>
        
        <var-divider v-show="flag" description="历史记录" /></div>
    </div>
    <div class="setproblem">
      <div class="sendCtx">
        <var-input class="text" variant="outlined" @keyup.enter="sentMsg" placeholder="请输入内容" size="small"
          v-model="value" /><span @click="sentMsg">发送</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">

import { ref, watch } from 'vue'
import { kefu } from '@/api/home'
import { Dialog } from '@varlet/ui'
const value = ref('')
const RobotValue = ref('')
let id = (0) as number
const timer = ref<number | undefined>(undefined);
const flag = ref<boolean>(true)



const getTodayTime = () => {
  // 获取当前时间
  var day = new Date();
  let seconds = day.getSeconds().toString();
  if (Number(seconds) < 10) {

    seconds = "0" + seconds;
  } else {
    seconds = seconds;
  }
  let minutes = day.getMinutes().toString();
  if (Number(minutes) < 10) {
    minutes = "0" + minutes;
  } else {
    minutes = minutes;
  }
  let time =
    day.getFullYear() +
    "-" +
    (day.getMonth() + 1) +
    "-" +
    day.getDate() +
    " " +
    day.getHours() +
    ":" +
    minutes +
    ":" +
    seconds;
  return time;
}


const robot = ref<Array<ctx>>([
  {
    index: 0,
    content: "您好，智能助理为你服务",
    time: getTodayTime(),
    type: "robot"

  }
])


const onClickLeft = () => history.back();

//发送消息
const sentMsg = async () => {
  flag.value = false
  let res = value.value


  if (value.value) {
    id = id + 1
    let obj = {
      index: id,
      content: value.value,
      time: getTodayTime(),
      type: "self"
    }
    robot.value.push(obj)
  } else {
    Dialog("请输入内容")
    return
  }
  value.value = ''

  let data = await kefu(res)
  value.value = ''

  if (data.result == 0) {
    RobotValue.value = data.content

    getData()
  } else {
    id = id + 1
    let obj = {
      index: id,
      content: "网络错误",
      time: getTodayTime(),
      type: "robot"
    }
    robot.value.push(obj)
  }
}

//收到消息
const getData = () => {
  clearTimeout(timer.value)
  timer.value = setTimeout(endMsg, 1000 * 60)
  id = id + 1
  let obj = {
    index: id,
    content: RobotValue.value,
    time: getTodayTime(),
    type: "robot"
  }
  robot.value.push(obj)
}

interface ctx {
  index: number
  content: string
  time: string
  type: string
}

watch(() => [value.value, RobotValue.value], () => {
  scrollToBottom()
})


// 滚动到底部
const chatContainer = ref<HTMLElement>()
const scrollToBottom = () => {
  chatContainer.value!.scrollIntoView()
}
const endMsg = () => {
  flag.value = true
  id = id + 1
  let obj = {
    index: id,
    content: "退下吧",
    time: getTodayTime(),
    type: "robot"
  }
  robot.value.push(obj)
}

</script>

<style scoped lang="scss">
.box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: white;
  z-index: 999;
}

.setproblem {
  width: 100vw;
  position: absolute;
  border-top: 1px solid rgba(17, 27, 37, 0.2);

  bottom: 15px;


  .sendCtx {
    width: 100vw;
    height: 50px;
    overflow: hidden;
    background-color: white;
    display: flex;
    justify-content: space-between;
    padding: 5px 15px;
    box-sizing: border-box;

    .text {
      flex: 0 0 70vw;
    }

    span {
      display: block;
      width: 80px;
      height: 40px;
      line-height: 40px;
      background-color: #3a7afe;
      text-align: center;
      border-radius: 15px;
      color: white;

    }
  }


}

.content {
  position: fixed;
  top: 46px;
  left: 0;
  width: 100vw;
  height: calc(100vh - 96px);
  background-color: #f7fafc;
  overflow: auto;

  &::-webkit-scrollbar {
    width: 0;
  }
}

.item {
  margin-top: 12px;

  .robot {
    display: flex;



    .robotCtx {
      .ct {
        background-color: #fff;
        padding: 5px 8px;
        font-size: 14px;
        margin-right: 8px;
        border-radius: 10px;
        line-height: 52px;
        font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
      }


    }

  }

  .self {
    display: flex;
    flex-direction: row-reverse;

    .selfCtx {
      .ct {
        background-color: #fff;
        padding: 5px 8px;
        font-size: 14px;
        border-radius: 10px;

        margin-right: 8px;
        line-height: 52px;
        font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
      }


    }

  }

}

.history {
  margin-bottom: 60px;
}
</style>